﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using NetMicro.Swagger.Extensions;
using NetMicro.Webs.Middlewares;

namespace NetMicro.Host
{
    /// <summary>
    /// 
    /// </summary>
    public static class ApplicationBuilderExtensions
    {
        /// <summary>
        /// 启用WebHost
        /// </summary>
        /// <param name="app"></param>
        /// <param name="applicationBuilderOptions"></param>
        /// <param name="env"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseWebHost(this IApplicationBuilder app, ApplicationBuilderOptions applicationBuilderOptions, IHostEnvironment env)
        {
            applicationBuilderOptions = applicationBuilderOptions ?? new ApplicationBuilderOptions();

            //swagger中间件
            app.UseSwaggers();

            //启用请求body解析中间件
            app.UseMiddleware<RequestBodyMiddleware>();
            //请求id
            // app.UseMiddleware<RequestIdMiddleware>();

           
            //路由
            app.UseRouting();
          
            if (applicationBuilderOptions.UseAuthentication)
                app.UseAuthentication();

            if (applicationBuilderOptions.UseAuthorization)
                app.UseAuthorization();
            //路由终结点映射
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/healthcheck", async context => await context.Response.WriteAsync("ok"));
                endpoints.MapControllers();
            });
            return app;
        }
    }
}
